<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="javatime" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href=<%=basePath%>
          <%@include file="../common_css.jsp"%>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
    <input type="hidden" name="id" id="id" value="${cusOrder.id}">
    <input type="hidden" name="inputUserId" id="inputUserId" value="${cusOrder.customer.user.id}">
    <input type="hidden" name="inputCusId" id="inputCusId" value="${cusOrder.customer.id}">
    <input type="hidden" name="inputCarId" id="inputCarId" value="${cusOrder.carId}">
    <input type="hidden" name="inputOrderState" id="inputOrderState" value="${cusOrder.orderState}">

    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">客户姓名</label>
            <div class="layui-input-block">
                <select name="cusId"  id="cusId" lay-filter="cusId"
                        <c:if test="${cusOrder.cusId != null}">disabled</c:if>
                >
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input"
                       lay-verify="phone" name="phone" id="phone"  value="${cusOrder.customer.phone}" readonly
                >
            </div>
        </div>
    </div>


    <div class="layui-form-item layui-row">
        <div class="layui-col-xs8">
            <label class="layui-form-label">试驾车辆</label>
            <div class="layui-input-block">
                <select name="carId"  id="carId" lay-filter="carId"
                        <c:if test="${cusOrder.cusId != null}">disabled</c:if>
                >
                    <option value="" >请选择</option>
                </select>
            </div>
        </div>
    </div>


    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">定金金额</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="deposit" name="deposit"
                    value="${cusOrder.deposit}"
                    readonly
                >
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">尾款金额</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="subsequent" name="subsequent"
                       value="${cusOrder.subsequent}"
                       readonly
                >
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-row">

        <div class="layui-col-xs6">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-block">
                <select name="orderState"  id="orderState"
                        <c:if test="${cusOrder.cusId != null}">disabled</c:if>
                >
                    <option value="" >请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-col-xs6">
            <label class="layui-form-label">分配员工</label>
            <div class="layui-input-block">
                <select name="userId"  id="userId"
                        <c:if test="${cusOrder.cusId != null}">disabled</c:if>
                >
                    <option value="" >请选择</option>
                </select>
            </div>
        </div>
    </div>

    <br/>
    <c:if test="${cusOrder == null}">
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-lg" lay-submit=""
                        lay-filter="addOrUpdateCustomerServe">确认
                </button>
                <button class="layui-btn layui-btn-lg layui-btn-normal">取消</button>
            </div>
        </div>

    </c:if>

</form>
<script type="text/javascript">
    layui.use(['form', 'layer','laydate','upload'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery

        if($("input[name='inputUserId']").val()!=''){
            $.post("/user/findUser",function (res) {
                for(var i=0;i<res.length;i++){
                    if($("input[name='inputUserId']").val() == res[i].id){
                        $("#userId").append("<option value=\""+res[i].id+"\" selected  >"+res[i].name+"</option>");
                    }else{
                        $("#userId").append("<option value=\""+res[i].id+"\"   >"+res[i].name+"</option>");
                    }
                }
                layui.form.render("select");
            });
        }

        $.post("/customer/findCustomerByState",{state:3},function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputCusId']").val() == res[i].id){
                    $("#cusId").append("<option value=\""+res[i].id+"\"  selected >"+res[i].name+"</option>")
                }else{
                    $("#cusId").append("<option value=\""+res[i].id+"\"   >"+res[i].name+"</option>")
                }
            }
            layui.form.render("select");
        });

        form.on('select(cusId)', function(data){
            $.post("/user/findUserByCustomerId",{cusId:data.value},function (data) {
                $("#userId").append("<option value=\""+data.id+"\" selected  >"+data.name+"</option>");
                layui.form.render("select");
            })
            $.post("/customer/findCustomerPhoneById",{id:data.value},function (data){
                $("#phone").val(data)
            })
        });

        $.post("/car/findCarAll",function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputCarId']").val() == res[i].id){
                    $("#carId").append("<option value=\""+res[i].id+"\" selected  >"+res[i].series+" "+res[i].model+"</option>");
                }else{
                    $("#carId").append("<option value=\""+res[i].id+"\"   >"+res[i].series+" "+res[i].model+"</option>");
                }
            }
            layui.form.render("select");
        });

        form.on('select(carId)', function(data){
            $.post("/car/findCarById",{id:data.value},function (data){
                $("#deposit").val(5000)
                $("#subsequent").val(data.price - 5000)
            })
        });

        $.post("/dictionaries/findOrderState",function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputOrderState']").val() == res[i].valueId){
                    $("#orderState").append("<option value=\""+res[i].valueId+"\" selected  >"+res[i].valueName+"</option>");
                }else{
                    $("#orderState").append("<option value=\""+res[i].valueId+"\"   >"+res[i].valueName+"</option>");
                }
            }
            layui.form.render("select");
        });

        form.on('submit(addOrUpdateCustomerServe)',function (data) {
            var index= top.layer.msg("数据提交中,请稍后...",{icon:16,time:false,shade:0.8});
            var url = "/cusOrder/savaOrder";
            $.post(url,data.field,function (res) {
                if(res=='success'){
                    top.layer.msg("操作成功");
                    top.layer.close(index);
                    layer.closeAll("iframe");
                    // 刷新父页面
                    parent.location.reload();
                }
            });
            return false;
        });
    });
</script>
</body>
</html>